﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

namespace Nirvana
{
    [RequireComponent(typeof(Image))]
    [AddComponentMenu("Nirvana/UI/Language/Resource Load I18N")]
    [DisallowMultipleComponent]
    [ExecuteInEditMode]
    public sealed class ResLoadI18N : Translatable
    {

        [Serializable]
        private struct BindLanguageRes
        {
            [Tooltip("The languaget type.")]
            public I18NLanguageType languagetype;
            [Tooltip("The translate Res Name.")]
            public string translate;
        };

        /// <summary>
        /// 资源存放路径
        /// </summary>
        [Tooltip("Resource Path.")]
        [SerializeField]
        private string resPath = string.Empty;
        /// <summary>
        /// 内置翻译表，不通过keyid获取，直接设置
        /// </summary>
        [SerializeField]
        [Tooltip("The bind Language Res list.")]
        private List<BindLanguageRes> binds = new List<BindLanguageRes>();

        protected override void TranslateText()
        {
            Image img = this.GetComponent<Image>();
            if (img == null)
            {
                return;
            }

            this.UpdateText(img);
        }

        public override void GetTranslate(out string img)
        {
            img = null;
            I18NLanguageType language = I18N.Language;
            foreach (var f in binds)
            {
                if (f.languagetype == language)
                {
                    img = f.translate;
                    return;
                }
            }
        }

        private void UpdateText(Image img)
        {
            if (Application.isPlaying)
            {
                string msg;
                GetTranslate(out msg);
                if (msg != null)
                {
                    resPath = resPath.Replace('\\', '/');
                    string path = Path.Combine(resPath, msg);
                    img.sprite = Resources.Load<Sprite>(path);
                    img.SetNativeSize();
                }

            }
            else
            {

#if UNITY_EDITOR

                string msg;
                GetTranslate(out msg);
                if (!string.IsNullOrEmpty(msg))
                {
                    if (img.sprite == null)
                    {
                        resPath = resPath.Replace('\\', '/');
                        string path = Path.Combine(resPath, msg);
                        img.sprite = Resources.Load<Sprite>(path);
                        EditorUtility.SetDirty(img);

                    }
                    else if (msg != img.sprite.name)
                    {
                        resPath = resPath.Replace('\\', '/');
                        string path = Path.Combine(resPath, msg);
                        img.sprite = Resources.Load<Sprite>(path);
                        EditorUtility.SetDirty(img);
                    }
                    img.SetNativeSize();
                }

#endif
            }

        }

    }
}

